import WebSocket from "ws";

class Client {
  constructor() {
    const ws = new WebSocket("ws://localhost:8080");

    ws.on("open", () => {
      console.log("已连接到服务器");
    });

    ws.on("message", (message) => {
      console.log(`收到服务器消息: ${message}`);
    });

    ws.on("close", () => {
      console.log("连接已关闭");
    });

    ws.on("error", (error) => {
      console.error("WebSocket 错误:", error);
    });

    this.ws = ws;
  }

  send(data) {
    this.ws.send(JSON.stringify(data));
  }

  chat(msg) {
    this.send({ type: "chat", text: msg });
  }
}

const client = new Client();

setTimeout(() => {
  client.chat("hello word , 你是谁");
}, 500);
